package com.onlyxiahui.common.util;

/**
 * Date 2019-03-30 17:05:53<br>
 * Description
 *
 * @author XiaHui<br>
 * @since 1.0.0
 */
public class OnlyStringUtil {

    /**
     * Date 2019-03-30 17:05:41<br>
     * Description 替换字符串中的一节，从beginIndex开始到endIndex结尾，原字符串中的这一部分被替换成replace
     *
     * @param text
     * @param replace
     * @param beginIndex
     * @param endIndex
     * @return String
     * @since 1.0.0
     */
    public static String replace(String text, String replace, int beginIndex, int endIndex) {
        String value = null;
        if (null != text) {
            if (text.length() > endIndex && (beginIndex < endIndex)) {
                value = text.substring(0, beginIndex) + replace + text.substring(endIndex);
            } else {
                value = text;
            }
        }
        return value;
    }

    /**
     * Date 2019-03-30 17:06:32<br>
     * Description 替换前面部分字符为replace，保证后面endLength长度不被替换
     *
     * @param text
     * @param replace
     * @param replaceLength
     * @param endLength
     * @return String
     * @since 1.0.0
     */
    public static String replaceBefore(String text, String replace, int replaceLength, int endLength) {
        String value = null;
        if (null != text) {
            StringBuilder sb = new StringBuilder();
            char[] chars = text.toCharArray();
            int length = chars.length;
            int endIndex = (length - endLength);
            int beginIndex = (endIndex - replaceLength);
            beginIndex = (beginIndex < 0) ? 0 : beginIndex;
            for (int i = 0; i < length; i++) {
                if (beginIndex <= i && i < endIndex) {
                    sb.append(replace);
                } else {
                    sb.append(chars[i]);
                }
            }
            value = sb.toString();
        }
        return value;
    }

    /**
     * Date 2019-03-30 17:07:30<br>
     * Description 替换中间部分的字符串；保留前部分长度beginLength和后部分长度endLength；<br>
     * 中间的每一个字符都被替换成replace
     *
     * @param text
     * @param replace
     * @param beginLength
     * @param endLength
     * @return String
     * @since 1.0.0
     */
    public static String replaceMiddle(String text, String replace, int beginLength, int endLength) {
        String value = null;
        if (null != text) {
            StringBuilder sb = new StringBuilder();
            char[] chars = text.toCharArray();
            int length = chars.length;
            int endIndex = (length - endLength);
            for (int i = 0; i < length; i++) {
                if (beginLength <= i && i < endIndex) {
                    sb.append(replace);
                } else {
                    sb.append(chars[i]);
                }
            }
            value = sb.toString();
        }
        return value;
    }

    /**
     * Date 2019-03-30 17:07:42<br>
     * Description 替换一节字符，不管后面够不够，保留前面部分
     *
     * @param text
     * @param replace
     * @param beginIndex
     * @param endIndex
     * @return String
     * @since 1.0.0
     */
    public static String replaceAfter(String text, String replace, int beginIndex, int endIndex) {
        String value = null;
        if (null != text) {
            StringBuilder sb = new StringBuilder();
            char[] chars = text.toCharArray();
            int length = chars.length;
            for (int i = 0; i < length; i++) {
                if (beginIndex <= i && i <= endIndex) {
                    sb.append(replace);
                } else {
                    sb.append(chars[i]);
                }
            }
            value = sb.toString();
        }
        return value;
    }

    /**
     * Date 2019-03-30 17:08:12<br>
     * Description 获取字符串前面的数字
     *
     * @param text
     * @return String
     * @since 1.0.0
     */
    public static int getHeaderNumber(String text) {
        if (text == null || text.length() == 0) {
            return 0;
        }
        for (int i = 0; i < text.length(); i++) {
            char temp = text.charAt(i);
            if (temp >= '9' || temp <= '0') {
                if (i == 0) {
                    return 0;
                } else {
                    return Integer.parseInt(text.substring(0, i));
                }
            }
        }

        return Integer.parseInt(text);
    }

    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    public static String trim(final String text) {
        return text == null ? null : text.trim();
    }
}
